from time import sleep

from selenium.common import StaleElementReferenceException
from selenium.webdriver.remote.webelement import WebElement

from getXPath import  get_child_elements
from movement import MouseEvent
from login import Lt
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC  # 正确导入 expected_conditions
from selenium.webdriver.common.action_chains import ActionChains
# 如果你没有将 MicrosoftWebDriver.exe 添加到 PATH，请在这里指定其完整路径
service = EdgeService(executable_path='D:\浏览器驱动\edge驱动\msedgedriver.exe')

# 创建 Edge 选项对象，可选配置浏览器行为
edge_options = EdgeOptions()
# 例如，无头模式运行：
# edge_options.add_argument('headless')

# 创建 WebDriver 对象
driver = webdriver.Edge(service=service, options=edge_options)

try:
    # 打开网页
    driver.get('http://demo.codelover.club')  # 请替换为实际网址
    driver.fullscreen_window()
    # 登录
    login1 = Lt(driver)
    login1.log(23003025, 123456)
    print("登录成功")
    sleep(2)
    mouse_event = MouseEvent(driver)
    mouse_event.mouseHover(getXPath='/html/body/div[1]/div[1]/div/div/div[4]/div/div/div[2]')
    mouse_event.mouseClick(getXPath='/html/body/div[3]/div[2]/div[1]')
    # mouse_event.mouse_move_0()

    driver.implicitly_wait(10)  # 设置隐式等待时间为10秒


    for value in range(1,4):
        # 点击课程
        actions1 = ActionChains(driver)
        actions1.move_by_offset(0, 0).perform()
        actions1.click()
        mouse_event.mouseClick(getXPath=f'/html/body/div[1]/div[2]/div/div[4]/div/div/div[{value}]')
        mouse_event.mouseClick(getXPath='/html/body/div[1]/div[2]/div/div[3]/div[5]/div[2]/div/div')

        # 点击我的课程
        mouse_event.mouseHover(getXPath='/html/body/div[1]/div[1]/div/div/div[4]/div/div/div[2]')
        mouse_event.mouseClick(getXPath='/html/body/div[3]/div[2]/div[1]')



    # #
    # actions1 = ActionChains(driver)
    # actions1.move_by_offset(0, 0).perform()
    # actions1.click()
    # mouse_event.mouseClick(getXPath=f'/html/body/div[1]/div[2]/div/div[4]/div/div/div[1]')
    # mouse_event.mouseClick(getXPath='/html/body/div[1]/div[2]/div/div[3]/div[5]/div[2]/div/div')
    #
    # # 点击我的课程
    # mouse_event.mouseHover(getXPath='/html/body/div[1]/div[1]/div/div/div[4]/div/div/div[2]')
    # mouse_event.mouseClick(getXPath='/html/body/div[3]/div[2]/div[1]')

    # element = driver.find_element(By.CLASS_NAME('startLearning'))
    # driver.execute_script("arguments[0].scrollIntoView(true);", element)
    # mouse_event.mouseClick(getXPath="/html/body/div[1]/div[2]/div/div[3]/div[5]/div[2]/div/div/div[2]/div/div[2]/div/div[8]/div[1]/div/div[3]")
    # mouse_event.mouseClick(getXPath="/html/body/div[1]/div[2]/div/div[3]/div[5]/div[2]/div/div/div[2]/div/div[2]/div/div[7]/div[2]/div/div/div[2]/div/div[1]/div/div[3]")




    login1.quit()
    print("退出成功")


finally:
    # 完成所有操作后关闭浏览器
    driver.quit()


